package com.example.larou.Impl;

import cn.dev33.satoken.error.SaSpringBootErrorCode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.larou.config.StateEnum;
import com.example.larou.entity.StateVo;
import com.example.larou.entity.User;
import com.example.larou.mapper.UserMapper;
import com.example.larou.utils.Adapter;
import com.example.larou.utils.Result;
import com.example.larou.utils.SendEmailCode;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import javax.security.sasl.SaslException;
import java.time.Duration;

@Service
public class loginServiceImpl extends Adapter {
    @Resource
    private UserMapper userMapper;


    @Override
    public SaResult emailLoginService(User user) {
        String email=user.getEmail();
//        System.err.println(email);
        String password=user.getPassword();
        if (email.equals(userMapper.getEmail(email)) && password.equals(userMapper.getPassword(email))) {
            StpUtil.login(userMapper.getUser_id(email));
            System.err.println(StpUtil.getTokenInfo());
            System.err.println(StpUtil.getLoginIdByToken(StpUtil.getTokenValue()));
            return  SaResult.ok("账户登录成功").set("token",StpUtil.getTokenValue());
        }
        StpUtil.checkLogin();
        return SaResult.error("登录失败").setCode(400);
    }

    public  SaResult sendEmail(StateVo stateEnum) {
        if (SendEmailCode.sendEmail(stateEnum)) {
            return SaResult.ok("报警装置异常警告发送成功").setData(stateEnum.getStateEnum());
        }

        return SaResult.error("报警装置异常警告发送失败").setData(stateEnum.getStateEnum());
    }

}
